package ua.org.sovic.Widget.common;

import java.util.StringTokenizer;
import java.util.HashMap;
/**
*/
public class Util {
    private final static String Hex = "0123456789abcdef";
	/**
	*/
	public static String ByteStr(byte temp) {
		char Data[] = new char[2];
        Data[0] = Hex.charAt((temp & 0xF0) >>> 4);
		Data[1] = Hex.charAt(temp & 0x0F);
		return new String(Data);
	}
	/**
	*/
 	public static String ShortStr(short temp) {
		byte tempHi = (byte) ((temp & 0xFF00) >>> 8);
		byte tempLo = (byte) (temp & 0x00FF);
		return new String(ByteStr(tempHi)+ByteStr(tempLo));
	}
	/**
	*/
 	public static String IntStr(int temp) {
      	String Data = Integer.toString(temp);
		for (int i=Data.length(); i<8; i++) Data = "0" + Data;
		return Data;
	}
 	public static String Int2Str(int temp) {
      	String Data = Integer.toString(temp);
		for (int i=Data.length(); i<2; i++) Data = "0" + Data;
		return Data;
	}
	/**
	*/
	public static int HexInt(byte temp) {
		int Data = temp & 0x7F;
		if ((temp & 0x80)==0x80) Data |= 0x80;
		return Data;
	}
	/**
	*/
	public static byte StrByte(String str) {
			return (byte) StrShort(str);
	}
	/**
	*/
	public static short StrShort(String str) {
		short temp = 0; 
	    for (int i=0; i<str.length(); i++) {
	    	int b = Character.digit(str.charAt(i),16);
	    	temp <<= 4;
	    	temp |= b;
	    }
		return temp;
	}
	/**
	*/
	public static synchronized int StrInt(String str) {
		return Integer.valueOf(str).intValue();
	}
	/**
	*/
	public static byte[] StrByteArray(String str) {
		byte[] tmp = new byte[str.length()/2];
			int j = 0;
			for (int i=0; i<str.length(); i++) {
				tmp[j]=StrByte(new String(str.charAt(i)+""+str.charAt(i+1)));
				i++;
				j++;
			}
		return tmp;
	}
	/**
	*/
	public static String ArrayByteStr(byte[] tmp) {
		String str = "";
			for (int i=0; i<tmp.length; i++) {
				str += ByteStr(tmp[i]);
			}
		return str;
	}
	public static String isByte(int b) {
		String str;
		str = Integer.toString(b);
		if (str.length()<2) str = "0" + str;
		return str;
	}
	public static String[] getArray(String arg) {
		StringTokenizer	stArg = new StringTokenizer(arg,",");
		int count = stArg.countTokens();
		String[] tmpArray = new String[count];
		for (int i=0; i<count; i++) {
			tmpArray[i] = stArg.nextToken();
		}
		return tmpArray;
	}

// Parce SQL ,,, $NameVar <- vatiable.put(NameVar, Value);        
        public static String parseSQL(String sql, HashMap variable) {
        StringBuffer temp = new StringBuffer(sql);
        int index = 0;
        while (index<temp.length()) {
            int start = temp.indexOf("$", index);
            if (start == -1) break;
            else {
                int end = (temp.indexOf("'", start)==-1)?((temp.indexOf(" ", start)==-1)?temp.indexOf(")", start):temp.indexOf(" ", start)):temp.indexOf("'", start);
                if (end == -1) {
                    String var = temp.substring(start+1, temp.length());                  
                    break;
                }
                String var = temp.substring(start+1, end);
                System.out.println(var);
                temp = temp.replace(start, end, (String)variable.get(var));
                index = end;
            }
        }
        return new String(temp);
    }

}